<template>
<div>
  <div>
    <el-table
        :data="tableData"
        stripe
        style="width: 100%">
      <el-table-column
          prop="height"
          label="高度"
          width="80">
      </el-table-column>
      <el-table-column
          prop="hash"
          label="区块哈希"
          width="280">
      </el-table-column>
      <el-table-column
          prop="address"
          label="矿工地址">
      </el-table-column>
    </el-table>
  </div>

  <div class="mt-16 ml-2">
    <el-button type="primary" @click="startmine">开始挖矿</el-button>

    <el-button type="danger" class="inline-block">停止挖矿</el-button>

    <div class="inline-block ml-2 align-middle">
      <div class="stat">
        <div class="stat-title">账户余额</div>
        <div class="stat-value">${{money}}</div>
<!--        <div class="stat-actions">-->
<!--          <button class="btn btn-sm btn-success">Add funds</button>-->
<!--        </div>-->
      </div>
    </div>
    <div>
      <ul>
        <li v-for="i in hashs">{{i}}</li>
      </ul>
    </div>
  </div>
  <div>
  </div>
</div>
</template>

<script>
import TeCoin from "../utils/TeCoin.json"
export default {
  name: "HomePage",
  created:async function(){
    // console.log(this.ethers)
    await window.ethereum.enable()
    const provider = new this.ethers.providers.Web3Provider(window.ethereum)
    this.$store.state.provider=provider
    let contractAddress="0x233194B6e7aBE10E4F7789E9Caa92051B621B11b"
    let contract = new this.ethers.Contract(contractAddress, TeCoin.abi, provider)
    console.log(contract)
    this.$store.state.contract=contract
    // console.log(await contract.owner())
    // console.log(await contract.name())
    let userAddress=await provider.getSigner().getAddress()
    console.log(typeof userAddress)
    this.$store.state.userAddress=userAddress
    this.money=await contract.balanceOf(userAddress)
    // console.log(this.money)
    let a=await contract.get_data()
    let singer=await provider.getSigner()

    let address_others="0x4EdBA1Ad4F9d3C3397af2177bDCb37AE0eDeF2A9"

    const contract_singner=contract.connect(singer)
    console.log(contract_singner)
    contract_singner.transfer(address_others,10)

    console.log(a[1].toNumber())
    // console.log(typeof a[2])
    // console.log()
    let nonce=0
    while(true){
      let non_hash=this.ethers.utils.solidityKeccak256(["uint","uint","uint","bytes32","address","uint"],[a[0].toNumber(),a[1].toNumber(),nonce,a[2],userAddress,100])
      let non=non_hash.slice(0,5)
      if(non=="0x000"){
        console.log(non_hash)
        break
      }else{
        nonce+=1
        this.hashs.shift()
        this.hashs.push(non_hash)
        // this.$set(this,"hashs",this.hashs)
        // console.log(this)
        console.log(non_hash)
        console.log(nonce)

        await contract_singner.mint(nonce)

      }
    }
  },
  data() {
    return {
      tableData: [{
        height: '1',
        hash: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        height: '2',
        hash: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        height: '3',
        hash: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        height: '4',
        hash: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }],
      money:0,
      hashs:[0,0,0,0,0,0,0,0,0,0]
    }
  },
  methods:{
    startmine(){
      for(let i=0;i<1000;i++){
        this.hashs.shift()
        this.hashs.push(i)
        console.log(this)
      }
    }
  }
}
</script>

<style scoped>

</style>